import java.io.*;
import java.net.*;
import java.util.*;

public class MyHttpGet {
        private String host;
        private int port;
        private String contextPath;
        private StringBuffer requestMessage;
        private Map<String,String> headers = new LinkedHashMap<>();
        private Map<String,String> params = new HashMap<>();

        public MyHttpGet(String address) {
                try {
                        URL url = new URL(address);
                        this.host= url.getHost();
                        this.port = url.getPort();
                        this.contextPath = url.getPath();
                        headers.put("Host",this.host);
                } catch (MalformedURLException e) {
                        e.printStackTrace();
                }
        }


        public  InputStream execute(){
                StringBuffer requestMessage = new StringBuffer();
                List<String> pairs = new ArrayList<>();
                // 处理查询参数
                params.keySet().stream().forEach(key->{
                        StringBuffer pair = new StringBuffer();
                        pair.append(key);
                        pair.append("=");
                        pair.append(params.get(key));
                        pairs.add(pair.toString());
                });

                if(params.size() > 0){
                        this.contextPath = this.contextPath + "?" + String.join("&", (String[])pairs.toArray());
                }

                // http 协议内容
                requestMessage.append("GET " + this.contextPath + " HTTP/1.1" + System.getProperty("line.separator"));
                for(String key: headers.keySet()){
                        requestMessage.append(key);
                        requestMessage.append(": ");
                        requestMessage.append(headers.get(key));
                        requestMessage.append(System.getProperty("line.separator"));
                }

                requestMessage.append("\r\n");

                Socket socket = new Socket();
                SocketAddress socketAddress = new InetSocketAddress(this.host,this.port);
                try {
                        socket.connect(socketAddress);
                } catch (IOException e) {
                        e.printStackTrace();
                }

                BufferedWriter bufferedWriter = null;
                try {
                        bufferedWriter = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
                        System.out.println("request================="+requestMessage.toString());

                        bufferedWriter.write(requestMessage.toString());
                        bufferedWriter.flush();
                        return socket.getInputStream();
                } catch (IOException e) {
                        e.printStackTrace();
                }
                return null;
        }


        public MyHttpGet addParam(String key, String value){
                params.put(key, value);
                return this;
        }

        public MyHttpGet addHeader(String key, String value){
                headers.put(key, value);
                return this;
        }

        public static void main(String[] args){
                MyHttpGet myHttpGet = new MyHttpGet("http://localhost:8080/myServlet");
                myHttpGet.addHeader("Connection", "keep-alive")
                        .addHeader("Cache-Control", "max-age=0")
                        .addHeader("Upgrade-Insecure-Requests", "1")
                        .addHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.99 Safari/537.36")
                        .addHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8")
                        .addHeader("Accept-Encoding", "gzip, deflate, sdch")
                        .addHeader("Accept-Language", "en-US,en;q=0.8,zh-CN;q=0.6,zh;q=0.4");

                InputStream inputStream = null;
                try {
                        inputStream = myHttpGet.execute();
                        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
                        String line;
                        while((line = bufferedReader.readLine()) != null){
                                System.out.print(line + "\r\n响应===============================");
                        }
                        inputStream.close();
                } catch (IOException e) {
                        e.printStackTrace();
                }
        }

}
